package com.dlc.felear.xiaoensale.utils;

import com.softwinner.Gpio;

public class PwmControlUtil {
    private static final String TAG = "PwmControlUtil";
    private static PwmControlUtil instance;

    public static PwmControlUtil getInstance() {
        if (instance == null) {
            synchronized (PwmControlUtil.class) {
                if (instance == null) {
                    instance = new PwmControlUtil();
                }
            }
        }
        return instance;
    }

    private PwmControlUtil() {
        initIoModel();
        loop();
    }

    private void initIoModel() {
        Gpio.setMulSel('c', 0, 1);
        Gpio.setMulSel('c', 2, 1);
        Gpio.setMulSel('c', 3, 1);
        Gpio.setMulSel('m', 4, 1);
        openNum(0, 1);
        openNum(2, 1);
    }

    private void loop() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        openNum(4, 1);
                        delay(1);
                        openNum(4, 0);
                        delay(1);
                    } catch (Exception e) {

                    }
                }

            }
        }).start();
    }

    private void delay(int delay) throws InterruptedException {

        Thread.sleep(delay);
    }

    public void openNum(int num, int value) {
        if (num < 4) {
            Gpio.writeGpio('c', num, value);
        } else {
            Gpio.writeGpio('m', num, value);
        }
    }
}
